package cn.onlineclass.service;

import cn.onlineclass.dao.CollectMapper;
import cn.onlineclass.entity.OCollect;
import cn.onlineclass.util.MybatisUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.onlineclass.vo.AjaxPage;
import cn.onlineclass.vo.AjaxResult;

import java.util.List;

public class CollectService {
    //记录日期log4g
    private static Logger Log= LoggerFactory.getLogger(CollectService.class);

    public static final CollectService COS=new CollectService();

    public List<OCollect> findALL(){
        SqlSession s= MybatisUtil.get();
        try {
            CollectMapper mapper = s.getMapper(CollectMapper.class);
            return mapper.findALL();
        }finally {
            MybatisUtil.close(s);
        }
    }

    //根据id查找单个Student信息
    //根据主键找单个对象的查询方法
    public OCollect get(long id){
        SqlSession s =MybatisUtil.get(true);
        try {
            CollectMapper mapper = s.getMapper(CollectMapper.class);
            return mapper.get(id);
        }finally {
            MybatisUtil.close(s);
        }
    }

    public AjaxResult add(OCollect c){
        SqlSession s= MybatisUtil.get();
        try {
            CollectMapper mapper = s.getMapper(CollectMapper.class);
            return AjaxResult.success(mapper.add(c));
        }catch (Exception e){
            Log.error("增加失败",e);
            return AjaxResult.success(0);
        }finally {
            MybatisUtil.close(s);
        }
    }

    public AjaxResult update(OCollect c){
        SqlSession s= MybatisUtil.get();
        try {
            CollectMapper mapper = s.getMapper(CollectMapper.class);
            return AjaxResult.success( mapper.update(c));
        }finally {
            MybatisUtil.close(s);
        }
    }

    public AjaxResult del(long id){
        SqlSession sqlSession= MybatisUtil.get();
        try {
            CollectMapper mapper = sqlSession.getMapper(CollectMapper.class);
            return AjaxResult.success(mapper.del(id));
        }finally {
            MybatisUtil.close(sqlSession);
        }
    }
    public AjaxPage search(@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("openid") String openid){
        SqlSession s= MybatisUtil.get();
        try {
            CollectMapper mapper = s.getMapper(CollectMapper.class);
            List<OCollect> search = mapper.search(pageNo, pageSize, openid);
            int total = mapper.searchTotal(openid);
            return new AjaxPage(search,total,10,pageNo);
        }finally {
            MybatisUtil.close(s);
        }
    }

}
